package Square;

import java.math.BigDecimal;

import Border.BorderType;
import Border.Direction;

public class TransparantSquare extends DefaultSquare {

	/**
	 * Constructor of the transparantSquares
	 * 
	 * @param 		temperature
	 * 				The temperature of the transparantsquare
	 * 
	 * @param 		humidity
	 * 				The humidity of the transparantsquare
	 * 
	 * @param 		directions
	 * 				Directions in which a closed door is added
	 * 
	 * @throws 		IllegalArgumentException
	 * 				Exception that is thrown if the length of directions is smaller then 1 or larger then 2.
	 * 				| (directions.length == 0 || directions.length > 2)
	 * 
	 * @throws		IllegalArgumentException
	 * 				Exception that is thrown if the length of directions is equal to 2, but the doors aren't located
	 * 				in opposite directions.
	 * 				| (directions.length == 2 && (! checkRightDoorPosition(directions[0], directions[1]) ) )
	 * 
	 * @effect		This TransparantSquare is initialized as a new DefaultSquare with the given temperature and humidity
	 * 				| super(temperature, humidity)
	 * 
	 * @post 		The bordertypes in the given directions are adjusted to 'CLOSED_DOOR'
	 * 				| for each direction in directions
	 * 				| 		new.getBorderAt(direction).getBordertype() == CLOSED_DOOR
	 */
	public TransparantSquare(double temperature, BigDecimal humidity,Direction... directions)
			throws IllegalArgumentException {
		super(temperature, humidity);
		if(directions.length == 0 || directions.length > 2)
			 throw new IllegalArgumentException();
		if(directions.length == 2 && (! checkRightDoorPosition(directions[0], directions[1])) )
			throw new IllegalArgumentException();
		for (Direction direction : directions)
			this.getBorderAt(direction).setBorderType(BorderType.CLOSED_DOOR);
	}
	
	/**
	 * Method that checks if the 2 doors are placed in the right direction
	 * 
	 * @param 		direction1
	 * 				The first direction of the Border
	 * 
	 * @param 		direction2
	 * 				The second direction of the border
	 * 
	 * @return		Returns true if the borders are oposite to eachother and false if not.
	 * 				| result == (direction1.getOpposite() == direction2)
	 */
	public boolean checkRightDoorPosition(Direction direction1 ,Direction direction2){
		return (direction1.getOpposite() == direction2);
	}

	/**
	 * @param	type
	 * 			The bordertype that the border will be set to
	 * @param	direction
	 * 			The direction of the border that will be modified
	 * @return	Returns false if broder type is BorderType.WALL
	 * 			Returns false if the change will result in the square having more then 2 doors
	 *  		Returns false if change results in the square having 2 doors which aren't in opposite directions
	 *  		Returns false if the change results in the square having a door in the floor
	 *  		Returns true in all other cases
	 * 			| if(type == BorderType.WALL)
	 *			| 	result == false
	 *			| if (countAmountOfDoors() == 2 && !getBorderAt(direction).isDoor() && (type == BorderType.OPEN_DOOR || type == BorderType.CLOSED_DOOR))
	 *			| 	result == false
	 *			| if (countAmountOfDoors() == 1 && !getBorderAt(direction).isDoor() && (type == BorderType.OPEN_DOOR || type == BorderType.CLOSED_DOOR) && !getBorderAt(direction.getOpposite()).isDoor())
	 *			|	result == false
	 *			| if((type == BorderType.OPEN_DOOR || type == BorderType.CLOSED_DOOR) && direction == Direction.FLOOR)
	 *			|	result == false
	 *			| result == true
	 */
	@Override
	public boolean canHaveAsBorderType(BorderType type, Direction direction) {
		if(type == BorderType.WALL)
			return false;
		if (countAmountOfDoors() == 2 && !getBorderAt(direction).isDoor() && (type == BorderType.OPEN_DOOR || type == BorderType.CLOSED_DOOR))
			return false;
		if (countAmountOfDoors() == 1 && !getBorderAt(direction).isDoor() && (type == BorderType.OPEN_DOOR || type == BorderType.CLOSED_DOOR) && !getBorderAt(direction.getOpposite()).isDoor())
			return false;
		if((type == BorderType.OPEN_DOOR || type == BorderType.CLOSED_DOOR) && direction == Direction.FLOOR)
			return false;
		
		return true;
	}
	
}
